package com.ldg.crm.controller;

import java.util.Date;
import java.util.List;

import javax.enterprise.inject.Default;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ldg.crm.common.CommonResult;
import com.ldg.crm.dto.EmployeeDto;
import com.ldg.crm.entity.EmployeeEntity;
import com.ldg.crm.entity.UserEntity;
import com.ldg.crm.service.EmployeeService;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

	@Autowired
	EmployeeService employeeService;

	@RequestMapping("add")
	@ResponseBody
	public CommonResult<EmployeeEntity> addEmployee(EmployeeEntity employee) {

		CommonResult<EmployeeEntity> result = null;

		if (employeeService.addEmployee(employee) == 1) {
			result = CommonResult.success(employee, "添加员工操作 成功！");
		} else {
			result = CommonResult.failed("添加员工操作 失败！");
		}

		return result;
	}

	@RequestMapping("modif")
	@ResponseBody
	public CommonResult<EmployeeEntity> modifEmployee(EmployeeEntity employee) {
		System.err.println(employee);

		CommonResult<EmployeeEntity> result = null;

		if (employeeService.updateEmployee(employee) == 1) {
			result = CommonResult.success("修改员工操作 成功！");
		} else {
			result = CommonResult.failed("修改员工操作 失败！");
		}

		return result;
	}

	@RequestMapping("remove")
	@ResponseBody
	public CommonResult<EmployeeEntity> removeEmployee(Integer[] ids) {
		CommonResult<EmployeeEntity> result = null;

		if ( employeeService.deleteEmployee(ids) > 0) {
			result = CommonResult.success("删除员工操作 成功！");
		} else {
			result = CommonResult.failed("删除员工操作 失败！");
		}

		return result;
	}

	//false 可有可无 
	@RequestMapping("list")
	@ResponseBody
	public EmployeeDto findEmployeeAll1(@RequestParam(value = "page", required = false, defaultValue = "1") int pageNum,
			@RequestParam(value = "rows", required = false, defaultValue = "20") int pageSize,@RequestParam( required = false) String queryParamUser,@RequestParam( required = false) Date queryParamHireDate) {
		//@RequestParam( required = false) String searchFactor,
		return employeeService.findPageList1(queryParamUser,queryParamHireDate,pageNum, pageSize);
	}
	
	@RequestMapping("create")
	@ResponseBody
	public CommonResult<EmployeeEntity> createUser(EmployeeEntity employee) {

		CommonResult<EmployeeEntity> result = null;
		int i = employeeService.createUser(employee);
		
		if (i==1) {
			result = CommonResult.success(employee, "添加员工操作 成功！");
		} else{
			result = CommonResult.failed("该员工已存在！");
		}

		return result;
	}
}
